<template>
    <div class="userglobal">
        <n-form ref="formRef" :model="model" :rules="rules" label-placement="left" label-width="auto">
            <n-form-item label="旧密码">
                <n-input v-model:value="model.opassword" type="password"  placeholder="请输入" />
            </n-form-item>
         
            <n-form-item label="新密码" path="password">
                <n-input v-model:value="model.password" type="password" placeholder="请输入" />
            </n-form-item>
                     
            <n-form-item label="确认密码" path="repassword">
                <n-input v-model:value="model.repassword" type="password" placeholder="请输入" />
            </n-form-item>
          
            <n-form-item>
                <n-button type="success" @click="submit()">提交修改</n-button>
            </n-form-item>

        </n-form>
    </div>
</template>
<script setup>
import { NForm, NFormItem, NButton, NInput, NUpload, NRadioGroup, NRadio, NSpace } from 'naive-ui';
import { getCurrentInstance } from "vue";
import { useRouter } from 'nuxt/app';
import { createDiscreteApi } from "naive-ui";
let { ctx: that, proxy } = getCurrentInstance()
onMounted(() => {
    console.log("修改资料")
    token.value = window.localStorage.getItem("token")
    getinfo()
})
const getinfo = async () => {
    const { data } = await useHttp("", "/getinfo");
    console.log(data)
    model.value.opassword = data.value.password
}
const fileList = ref([]);
const model = ref({
    opassword: "",
    password: "",
    repassword: "",
   
});
const token = ref("");

const rules = {
    repassword: {
        required: true,
        trigger: ['blur', 'input'],
        message: '请输入确认密码'
    },
    password: {
        required: true,
        trigger: ['blur', 'input'],
        message: '请输入确认密码'
    }
}
const submit = async () => {
  const { message, dialog } = createDiscreteApi(['message', 'dialog'])

    if (model.value.password == '' || model.value.repassword == '') {
        
        message.warning("请输入必填项")
        return
    }
     if (model.value.password !== model.value.repassword) {
        message.warning("两次输入密码不一致");
        return
    }
    const { data ,error} = await useHttp("", "/update_password", {
        method: "post",
        body: model.value
    })
     if (data.value == undefined) {
        // setTimeout(() => {
        //     console.log(error)
        // }, 100)
        // console.log("12333")
        message.warning(error.value.data.data);
    } else {
       that.$forceUpdate()
    message.success("成功");
    getinfo()




    }


   


}
</script>
<style scoped>
.userglobal {
    width: 100%;
    height: 100%;
    padding: 20px;
    box-sizing: border-box;
}
</style>